﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
using System.IO;


namespace EncryptDecrypt
{

    public class CryptorEngine
    {
        public static SymmetricAlgorithm mCSP;
        private const string CIV = "123$%^ab";  //密钥, 只能是8位  
        private const string CKEY = "abc123&*"; //初始化向量， 只能是8位  

        public CryptorEngine()
        {
            mCSP = new DESCryptoServiceProvider();
        }


        /// <summary>加密字符串  
        ///   
        /// </summary>  
        /// <param name="Value"></param>  
        /// <returns></returns>  
        public static string EncryptString(string Value)
        {
            mCSP = new DESCryptoServiceProvider();
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            // 这里视频上用的是Convert.FromBase64String(CKEY/CIV),但是我用的话总是出现错误，什么大小不对  
            ct = mCSP.CreateEncryptor(Encoding.ASCII.GetBytes(CKEY), Encoding.ASCII.GetBytes(CIV));
            byt = Encoding.UTF8.GetBytes(Value);
            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            return Convert.ToBase64String(ms.ToArray());
        }

        /// <summary>解密字符串  
        ///   
        /// </summary>  
        /// <param name="Value"></param>  
        /// <returns></returns>  
        public static string DecryptString(string Value)
        {
            mCSP = new DESCryptoServiceProvider();
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            ct = mCSP.CreateDecryptor(Encoding.ASCII.GetBytes(CKEY), Encoding.ASCII.GetBytes(CIV));
            byt = Convert.FromBase64String(Value);

            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();

            return Encoding.UTF8.GetString(ms.ToArray());
        }


        /// <summary>
        /// MD5 16位加密
        /// </summary>
        /// <param name="ConvertString"></param>
        /// <returns></returns>
        public static string GetMd5_16(string ConvertString)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
            t2 = t2.Replace("-", "");
            return t2;
        }

        /// MD5　32位加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetMd5_32(string str)
        {
            string cl = str;
            string pwd = "";
            MD5 md5 = MD5.Create();//实例化一个md5对像
            // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符

                pwd = pwd + s[i].ToString("X");

            }
            return pwd;
        }
    }



}
